<%@ taglib prefix="page" uri="http://com.qipay/pagelib" %>
<%@ page language="java" import="com.qipay.admin.withdraw.vo.WithdrawVo" contentType="text/html;charset=utf-8" %>
<%@ page import="com.qipay.admin.tags.PageModel" %>
<%@ page import="com.qipay.admin.utils.SiteHelper" %>
<%@ page import="com.qipay.user.entity.UserLevel" %>
<%@ page import="org.apache.commons.collections.CollectionUtils" %>
<%@ page import="java.util.List" %>
<%@ page import="com.qipay.withdraw.config.WithdrawStateEnum" %>
<%@ page import="java.util.Map" %>
<%@ page import="com.qipay.configuration.config.ConfigurationEnum" %>
<%@ page import="org.apache.commons.lang3.StringUtils" %>
<%@ page import="com.qipay.admin.withdraw.vo.CountWithdrawInfoVo" %>
<%@ page import="com.qipay.admin.withdraw.config.BwmConfig" %>
<!doctype html>
<html>
<head>
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <jsp:include page="head.jsp"></jsp:include>
    <link rel="stylesheet" href="<%= request.getAttribute("StaticHost") %>/css/dataTables.bootstrap.min.css">
    <link rel="stylesheet" href="<%= request.getAttribute("StaticHost") %>/css/bootstrap-datepicker3.min.css">
    <link rel="stylesheet" href="<%= request.getAttribute("StaticHost") %>/css/list.css">
    <title>提现列表</title>
    <script type="text/javascript">
        //document.getElementsByTagName("html")[0].style.width=screen.availWidth/100+"px";
        document.getElementsByTagName("html")[0].style.fontSize=screen.availWidth/200+"px";
    </script>


</head>
<body class="devpreview hold-transition skin-blue sidebar-mini">
<div class="wrapper">
    <jsp:include page="nav.jsp"></jsp:include>
    <jsp:include page="left.jsp"></jsp:include>
    <!-- Content Wrapper. Contains page content -->
    <div id="bbb0" style="display:none;position: fixed;height:250px;width:400px;margin:10% 35% 25% 30%;background: #fff;z-index: 11;">
        <div style="text-align: center;line-height: 50px;border-bottom: 1px solid #dcdcdc;font-size: 22px;margin-top: 10px;">
            选择代付通道
            <a onclick="closeChoose();" style="cursor:pointer;font-size:12px;padding:0px 20px;float:right;border-radius: 5px;">关闭</a>
        </div>
        <div id="canbanlance" style="margin-top: 5px;margin-left: 10%;color:red;padding-left: 20px;height: 15px;">

        </div>
        <div style="padding:20px;line-height: 30px;text-indent:25px;color: #000;text-align:center;">
            <select id="selectselect" onchange="changeAnPayCh();" style="height:35px;border: 1px solid #dcdcdc;border-radius: 3px;width: 80%;font-size: 16px;">
                <option value="0">请选择代付通道</option>
                <option accountType="<%=BwmConfig.accountType_Default%>" value="<%=BwmConfig.widthdraw%>">微信支付宝等其他代付</option>
                <option accountType="<%=BwmConfig.accountType_UnionPay%>" value="<%=BwmConfig.unionPayWidthdraw%>">银联二维码代付</option>
            </select>
            <div style="text-align:center;margin-top: 30px;" >
                <input onclick="linkPass();" id="submitsubmit" style="line-height:35px;width:80%;cursor:pointer;font-size: 18px;text-align:center;
	    background:#3385ff;color:#fff;border-radius:3px;border:none;" type="button" value="提交"/>
            </div>
        </div>
    </div>
    <div id="ccc0" style="display:none;position: fixed;background:#000;opacity: 0.5;height:100%;width:100%;z-index: 10;"></div>
    <div class="content-wrapper">
        <!-- Main content -->
        <section class="content">
            <div class="row">
                <div class="col-xs-12">
                    <div class="box border" >

                        <div>
                            <form action="${pageContext.request.contextPath}/withdraw" id="searchForm">
                                <%
                                    Long cpId = (Long) request.getAttribute("cpId");
                                    String orderNo = (String) request.getAttribute("orderNo");
                                    Byte state = (Byte) request.getAttribute("state");
                                    String from = (String) request.getAttribute("from");
                                    String to = (String) request.getAttribute("to");
                                    UserLevel userLevel = (UserLevel) request.getAttribute("level");
                                    CountWithdrawInfoVo countWithdrawInfoVo = (CountWithdrawInfoVo) request.getAttribute("countWithdrawInfoVo");
                                %>
                                <%if(userLevel.getCode()>UserLevel.CP.getCode()){%>
                                <div class="new-group">
                                    <label for="cpId">商户 ID：</label>
                                    <input type="text" class="" id="cpId" name="cpId" placeholder="商户 ID"
                                           value="<%= cpId!=null?cpId:""%>"/>
                                </div>
                                <%}%>

                                <div class="new-group date form_datetime">
                                    <label for="from">开始日期：</label>
                                    <input type="text" class="" id="from" name="from" readonly
                                           value="<%= StringUtils.isNotBlank(from) ?from:""%>">
                                </div>
                                <div class="new-group date form_datetime">
                                    <label for="to">结束日期：</label>
                                    <input type="text" class="" id="to" name="to" readonly
                                           value="<%= StringUtils.isNotBlank(to) ?to:""%>">
                                </div>

                                <div class="new-group">
                                    <label for="state">订单状态：</label>
                                    <select class="" id="state" name="state">
                                    <%
                                    Map<Byte, String> stateMap = (Map<Byte, String>) request.getAttribute("states");
                                      state= state!=null?state:(byte)-1;
                                    for (Byte key : stateMap.keySet()) {
                                    String value = stateMap.get(key);
                                    %>
                                    <option value="<%=key%>"
                                            <% if (state != null && state.equals(key)) {%>
                                            selected="selected"<%}%>>
                                        <%=value%>
                                    </option>
                                        <%}%>
                                    </select>
                                </div>
                                <div class="button-group">
                                    <button type=" submit" class="btn btn-primary btn-sm borderNone"
                                            id="btnSearch" >查询</button>
                                    <a href="" id="btnDownload" download="提现订单明细.xls" target="_blank">
                                        <button type="button" class="btn btn-primary btn-sm borderNone" >导出</button>
                                    </a>
                                </div>


                            </form>
                        </div>
                        <!-- /.box-header -->
                        <div class="tableContent">
                            <table id="pLst"  class="table table-bped table-hover">
                                <thead>
                                <tr>
                                    <th width="5%">ID</th>
                                    <%if(userLevel.getCode()>UserLevel.CP.getCode()){%>
                                    <th width="6%">商户 ID</th>
                                    <%--<th width="16%">订单号</th>--%>
                                    <%}%>
                                    <th width="8%">银行</th>
                                    <th width="8%">支行</th>
                                    <th width="15%">卡号</th>
                                    <th width="10%">姓名</th>
                                    <th width="6%">金额</th>
                                    <th width="8%">手续费</th>
                                    <th width="8%">到账金额</th>
                                    <th width="8%">状态</th>
                                    <%if (userLevel.equals(UserLevel.ADMIN)) {%>
                                    <th width="8%">备注</th>
                                    <%}%>
                                    <th width="15%">时间</th>
                                    <%if (userLevel.equals(UserLevel.ADMIN)) {%>
                                    <th width="21%">操作</th>
                                    <%}%>
                                </tr>
                                </thead>
                                <tbody>
                                <%
                                    List<WithdrawVo> list = (List<WithdrawVo>) request.getAttribute("lst");
                                    PageModel pager = (PageModel) request.getAttribute("page");
                                    Map<Byte, String> conf = (Map<Byte, String>) request.getAttribute("config");
                                    String url = pageContext.getRequest().getServletContext().getContextPath() + "/withdraw" + (StringUtils.isNotBlank(pager.getUrl()) ? pager.getUrl() : "");
                                    if (CollectionUtils.isNotEmpty(list)) {
                                        for (WithdrawVo p : list) {
                                %>
                                <tr id="p_<%=p.getId()%>">
                                    <td>
                                        <a href="/withdraw/<%= p.getId()%>" style="color: #00a65a">
                                            <%= p.getId()%>
                                        </a>
                                    </td>
                                    <%if(userLevel.getCode()>UserLevel.CP.getCode()){%>
                                    <td>
                                        <%= p.getCpId()%>
                                    </td>
                                    <%-- <td>
                                         系统订单</br>
                                         <%= p.getTradeNo()%></br>
                                         商户订单</br>
                                         <%= p.getOutTradeNo()==null?"":p.getOutTradeNo()%>
                                     </td>--%>
                                    <%}%>
                                    <td>
                                        <%= p.getCardBank()%>
                                    </td>
                                    <td>
                                        <%= p.getCardSubBank()%>
                                    </td>
                                    <td>
                                        <%= p.getCardNo()%>
                                    </td>
                                    <td>
                                        <%= p.getName()%>
                                    </td>
                                    <td>
                                        <%= SiteHelper.getMoneyFormatAll(Double.valueOf(p.getAmount()))%>
                                    </td>
                                    <td>
                                        <%= SiteHelper.getMoneyFormatAll(p.getFee())%>
                                    </td>
                                    <td>
                                        <%= SiteHelper.getMoneyFormatAll(Double.valueOf(p.getActualAmount()))%>
                                    </td>

                                    <td class="<%=(0== p.getState()? "warning":
                                    (WithdrawStateEnum.PAYED.getState()== p.getState()? "success":"danger"))%>">
                                        <%= p.getStateName()%>
                                    </td>
                                    <%if (userLevel.equals(UserLevel.ADMIN)) {%>
                                    <td><a onclick="updateRemark('<%= p.getId()%>')"  href="javascript:;" style="cursor: pointer;">
                                        <%= p.getRemark()%>
                                    </a></td>
                                    <%}%>
                                    <td>
                                        申请<%= SiteHelper.getDefaultTimeFormat(p.getCreateTime())%><br>
                                        <%= ("".equals(SiteHelper.getDefaultTimeFormat(p.getFinishTime()))?"":
                                                "完成"+SiteHelper.getDefaultTimeFormat(p.getFinishTime()))%>
                                    </td>

                                    <%
                                        if (userLevel.equals(UserLevel.ADMIN)) {
                                            if (p.getState().equals(WithdrawStateEnum.APPLY.getState())) {
                                    %>
                                    <td>
                                        <a name="lnkPass" pk="<%= p.getId()%>" withdrawBalance="<%= SiteHelper.getMoneyFormatAll(Double.valueOf(p.getActualAmount()))%>" href="javascript:;">
                                            <button type="button" class="btn btn-success btn-sm borderNone">出款</button></a>
                                        <a name="lnkHandPass" pk="<%= p.getId()%>" href="javascript:;">
                                            <button type="button" class="btn btn-warning btn-sm borderNone">手动出款</button></a>
                                        <%--<a name="lnkCancel" pk="<%= p.getId()%>" href="javascript:;" >
                                            <button type="button" class="btn btn-danger btn-sm borderNone">取消</button> </a>--%>
                                        <a name="lnkUnPass" pk="<%= p.getId()%>" href="javascript:;">
                                        <button type="button" class="btn btn-danger btn-sm borderNone">不通过</button></a>
                                    </td>
                                    <%}

                                        }
                                    %>

                                </tr>
                                <%
                                    }
                                } else {
                                %>
                                <tr>
                                    <td colspan="<%=(userLevel.equals(UserLevel.ADMIN))?15:14%>">未找到数据</td>
                                </tr>
                                <% }%>
                                </tbody>
                            </table>
                            <tr>
                                <td colspan="18">
                                    总金额：<%= SiteHelper.formatMoney(countWithdrawInfoVo.getAmountCount(), 100.0)%>
                                    &nbsp&nbsp
                                </td>
                                <td colspan="18">
                                    手续费：<%= SiteHelper.formatMoney(countWithdrawInfoVo.getFeeCount(), 100.0)%>&nbsp&nbsp
                                </td>
                                <td colspan="18">
                                    到账总金额：<%= SiteHelper.formatMoney(countWithdrawInfoVo.getActualAmountCount(), 100.0)%>
                                    &nbsp&nbsp
                                </td>
                                <td colspan="18">总笔数：<%= pager.getTotal()%>&nbsp&nbsp</td>
                            </tr>
                            <div class="col-sm-10">
                                <div class="dataTables_paginate paging_simple_numbers" id="pLst_paginate">
                                    <page:page
                                            totalSum="<%= pager.getTotal()%>"
                                            url="<%= url%>"
                                            pageNo="<%= pager.getPage()%>"
                                            pageSize="<%= pager.getPageSize()%>"/>
                                   </div>
                                </div>
                            </div>
                        <div style="clear: both"></div>
                        </div>
                        </div>
                    </div>
        </section>
    </div>


    <div class="modal fade" id="modal-default">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">Default Modal</h4>
                </div>
                <div class="modal-body">
                    <input placeholder="不通过的原因" id="modal-body" type="text"/>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default pull-left" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary">确定</button>
                </div>
            </div>
        </div>
    </div>

    <%--<jsp:include page="foot.jsp"></jsp:include>--%>
</div>
<input id="withdrawId" type="hidden" value="0"/>
<input id="withdrawBalance" type="hidden" value="0"/>
<input id="banlance" type="hidden" value="0"/>

<input id="agentNo" type="hidden" class="form-control" name="agentNo"
       value="<%=BwmConfig.agentNo%>">
<input id="accountType" type="hidden" class="form-control" name="accountType"
       value="<%=BwmConfig.accountType_Default%>">
<input id="trxType" type="hidden" class="form-control" name="trxType"
       value="">

<script src="<%= request.getAttribute("StaticHost") %>/js/jquery.dataTables.min.js"></script>
<script src="<%= request.getAttribute("StaticHost") %>/js/bootstrap-datepicker.min.js"></script>
<script src="<%= request.getAttribute("StaticHost") %>/js/bootstrap-datepicker.zh-CN.min.js"></script>
<script src="<%= request.getAttribute("StaticHost") %>/js/dataTables.bootstrap.min.js"></script>
<script src="<%= request.getAttribute("StaticHost") %>/js/adminlte.min.js"></script>

<script>
    function updateRemark (pId) {
        var remark = prompt("", "");
        if (null != remark && "" != remark) {
            $.ajax({
                url: "${pageContext.request.contextPath}/withdraw/" + pId + "/updateRemark?remark=" + remark,
                type: "PUT",
                async: true,
                context: this,
                timeout: 50000,
                success: function (data) {
                    if (null != data && data.code == 200) {
                        location.reload();
                    } else {
                        alert("失败:" + data.msg);
                    }
                },
                error: function (d, s, b) {
                    alert("失败:" + b);
                }
            });
        }
    }
    function changeAnPayCh(){
        var agentNo = $('#agentNo').val();
        var trxType = $('#selectselect option:selected').val();
        var accountType = $('#selectselect option:selected').attr("accountType");
        $('#accountType').val(accountType);$('#trxType').val(trxType);

        if(agentNo!="0"){
            $.ajax({
                url: "${pageContext.request.contextPath}/withdraw/queryCh?agentNo=" + agentNo +"&accountType="+accountType,
                type: "PUT",
                async: true,
                context: this,
                timeout: 50000,
                success: function (data) {
                    if (null != data && data.code == 200) {
                        var banlance = data.data;
                        $('#canbanlance').html("温馨提示：当前通道的余额为："+banlance);
                        $('#banlance').val(banlance);
                    } else {
                        alert("失败:" + data.msg);
                        $('#banlance').val("0");
                    }
                },
                error: function (d, s, b) {
                    //alert("失败:" + b);
                    $('#banlance').val("0");
                }
            });
        }else{
            $('#banlance').val("0");
        }
    }
    $("[name='lnkPass']").click(function () {
        $('#bbb0').show();
        $('#ccc0').show();
        var pId = $(this).attr("pk");
        var withdrawBalance = $(this).attr("withdrawBalance");
        $('#withdrawId').val(pId);
        $('#withdrawBalance').val(withdrawBalance);
    });
    function closeChoose(){
        $('#bbb0').hide();
        $('#ccc0').hide();
    }
    function linkPass(){
        var trxType = $('#trxType').val();
        var accountType = $('#accountType').val();

        var withdrawBalance = $('#withdrawBalance').val()*1;
        var banlance = $('#banlance').val()*1;
        if(banlance>=withdrawBalance){
            var pId = $('#withdrawId').val();
            var agentNo = $('#agentNo').val();
            if (confirm("确定审核通过吗？")) {
                $.ajax({
                    url: "${pageContext.request.contextPath}/withdraw/" + pId + "/pass?agentNo="+agentNo+"&trxType="+trxType+"&accountType="+accountType,
                    type: "PUT",
                    async: true,
                    context: this,
                    timeout: 50000,
                    success: function (data) {
                        if (null != data && data.code == 200) {
                            location.reload();
                        } else {
                            alert("失败:" + data.msg);
                            location.reload();
                        }

                    },
                    error: function (d, s, b) {
                        //alert("失败:" + b);
                    }
                });
            }
        }else{
            $('#canbanlance').html("温馨提示：当前通道的余额不足~");
        }

    }

    $(function () {
        $('#from, #to').datepicker({
            format: "yyyy-mm-dd",
            todayBtn: true,
            clearBtn: true,
            language: "zh-CN"
        });
        initDownload();
    });

    $("[name='lnkCancel']").click(function () {
        var pId = $(this).attr("pk");
        if (confirm("确定取消吗？")) {
            $.ajax({
                url: "${pageContext.request.contextPath}/withdraw/" + pId + "/cancel",
                type: "PUT",
                async: true,
                context: this,
                timeout: 50000,
                success: function (data) {
                    if (null != data && data.code == 200) {
                        location.reload();
                    } else {
                        alert("失败:" + data.msg);
                    }
                },
                error: function (d, s, b) {
                    alert("失败:" + b);
                }
            });
        }
    });

    $("[name='lnkUnPass']").click(function () {

        var pId = $(this).attr("pk");
        var remark = prompt("", "");

        if (null != remark && "" != remark) {
            $.ajax({
                url: "${pageContext.request.contextPath}/withdraw/" + pId + "/unPass?remark=" + remark,
                type: "PUT",
                async: true,
                context: this,
                timeout: 50000,
                success: function (data) {
                    if (null != data && data.code == 200) {
                        location.reload();
                    } else {
                        alert("失败:" + data.msg);
                    }
                },
                error: function (d, s, b) {
                    alert("失败:" + b);
                }
            });
        }
    });
    $("[name='lnkHandPass']").click(function () {

        var pId = $(this).attr("pk");
        var remark = prompt("", "");

        if (null != remark && "" != remark) {
            $.ajax({
                url: "${pageContext.request.contextPath}/withdraw/" + pId + "/handPass?remark=" + remark,
                type: "PUT",
                async: true,
                context: this,
                timeout: 50000,
                success: function (data) {
                    if (null != data && data.code == 200) {
                        location.reload();
                    } else {
                        alert("失败:" + data.msg);
                    }
                },
                error: function (d, s, b) {
                    alert("失败:" + b);
                }
            });
        }
    });
    $("#searchForm input, #searchForm select").change(initDownload);
    function initDownload() {
        $("#btnDownload").attr("href", "${pageContext.request.contextPath}/withdraw/export?" + $("#searchForm").serialize());
    }

</script>
</body>
</html>

